

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="转：PHP反射机制简介就算是类成员定义为 private 也可以在外部访问，不用创建类的实例也可以访问类的成员和方法。 PHP自5.0版本以后添加了反射机制，它提供了一套强大的反射API，允许你在PHP运行环境中，访问和使用类、方法、属性、参数和注释等，其功能十分强大，经常用于高扩展的PHP框架，自动加载插件，自动生成文档，甚至可以用来扩展PHP语言。由于它是PHP內建的oop扩展，为语言本身自带">
<meta property="og:type" content="article">
<meta property="og:title" content="PHP反射机制">
<meta property="og:url" content="http://example.com/2020/05/21/php/PHP%E5%8F%8D%E5%B0%84%E6%9C%BA%E5%88%B6/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="转：PHP反射机制简介就算是类成员定义为 private 也可以在外部访问，不用创建类的实例也可以访问类的成员和方法。 PHP自5.0版本以后添加了反射机制，它提供了一套强大的反射API，允许你在PHP运行环境中，访问和使用类、方法、属性、参数和注释等，其功能十分强大，经常用于高扩展的PHP框架，自动加载插件，自动生成文档，甚至可以用来扩展PHP语言。由于它是PHP內建的oop扩展，为语言本身自带">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-05-21T06:00:53.000Z">
<meta property="article:modified_time" content="2020-05-25T18:39:22.000Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="PHP">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>PHP反射机制 - Hexo</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.13","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>LZDF</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="PHP反射机制">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-05-21 14:00" pubdate>
        2020年5月21日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      4.1k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      35 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">PHP反射机制</h1>
            
            <div class="markdown-body">
              <h1 id="转：PHP反射机制"><a href="#转：PHP反射机制" class="headerlink" title="转：PHP反射机制"></a><a target="_blank" rel="noopener" href="https://www.cnblogs.com/daxiaohaha/p/11542374.html">转：PHP反射机制</a></h1><h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>就算是类成员定义为 <strong>private</strong> 也可以在外部访问，不用创建类的实例也可以访问类的成员和方法。</p>
<p>PHP自5.0版本以后添加了反射机制，它提供了一套强大的反射API，允许你在PHP运行环境中，访问和使用类、方法、属性、参数和注释等，其功能十分强大，经常用于高扩展的PHP框架，自动加载插件，自动生成文档，甚至可以用来扩展PHP语言。由于它是PHP內建的oop扩展，为语言本身自带的特性，所以不需要额外添加扩展或者配置就可以使用。更多内容见官方文档。</p>
<h1 id="反射类型"><a href="#反射类型" class="headerlink" title="反射类型"></a>反射类型</h1><p>PHP反射API会基于类，方法，属性，参数等维护相应的反射类，已提供相应的调用API。</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>Reflector</td>
<td>Reflector 是一个接口，被所有可导出的反射类所实现（implement）</td>
</tr>
<tr>
<td>Reflection</td>
<td>反射（reflection）类</td>
</tr>
<tr>
<td>ReflectionClass</td>
<td>报告了一个类的有关信息</td>
</tr>
<tr>
<td>ReflectionClassConstant</td>
<td>报告有关类常量的信息</td>
</tr>
<tr>
<td>ReflectionZendExtension</td>
<td>报告Zend扩展的相关信息</td>
</tr>
<tr>
<td>ReflectionExtension</td>
<td>报告了PHP扩展的有关信息</td>
</tr>
<tr>
<td>ReflectionFunction</td>
<td>报告了一个函数的有关信息</td>
</tr>
<tr>
<td>ReflectionFunctionAbstract</td>
<td>ReflectionFunction 的父类</td>
</tr>
<tr>
<td>ReflectionMethod</td>
<td>报告了一个方法的有关信息</td>
</tr>
<tr>
<td>ReflectionObject</td>
<td>报告了一个对象（object）的相关信息</td>
</tr>
<tr>
<td>ReflectionParameter</td>
<td>取回了函数或方法参数的相关信息</td>
</tr>
<tr>
<td>ReflectionProperty</td>
<td>报告了类的属性的相关信息</td>
</tr>
<tr>
<td>ReflectionType</td>
<td>获取函数、类方法的参数或者返回值的类型</td>
</tr>
<tr>
<td>ReflectionGenerator</td>
<td>获取生成器的信息</td>
</tr>
<tr>
<td>ReflectionException</td>
<td>反射异常类</td>
</tr>
</tbody></table>
<h1 id="访问"><a href="#访问" class="headerlink" title="访问"></a>访问</h1><p>假设定义了一个类 User，我们首先需要建立这个类的反射类实例，然后基于这个实例可以访问 User 中的属性或者方法。不管类中定义的成员权限声明是否为 public，都可以获取到。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><code class="hljs php"><span class="hljs-meta">&lt;?php</span> <br><span class="hljs-keyword">namespace</span> <span class="hljs-title">Extend</span>;<br><br><span class="hljs-keyword">use</span> <span class="hljs-title">ReflectionClass</span>;<br><span class="hljs-keyword">use</span> <span class="hljs-title">Exception</span>;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * Class User</span><br><span class="hljs-comment"> * <span class="hljs-doctag">@package</span> Extend</span><br><span class="hljs-comment"> */</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">User</span></span>&#123;<br>    <span class="hljs-keyword">const</span> ROLE = <span class="hljs-string">&#x27;Students&#x27;</span>;<br>    <span class="hljs-keyword">public</span> <span class="hljs-variable">$username</span> = <span class="hljs-string">&#x27;&#x27;</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$password</span> = <span class="hljs-string">&#x27;&#x27;</span>;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span>(<span class="hljs-params"><span class="hljs-variable">$username</span>, <span class="hljs-variable">$password</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">$this</span>-&gt;username = <span class="hljs-variable">$username</span>;<br>        <span class="hljs-keyword">$this</span>-&gt;password = <span class="hljs-variable">$password</span>;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获取用户名</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> string</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getUsername</span>(<span class="hljs-params"></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">$this</span>-&gt;username;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 设置用户名</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> string $username</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setUsername</span>(<span class="hljs-params"><span class="hljs-variable">$username</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">$this</span>-&gt;username = <span class="hljs-variable">$username</span>;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 获取密码</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> string</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getPassword</span>(<span class="hljs-params"></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">$this</span>-&gt;password;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 设置密码</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> string $password</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setPassowrd</span>(<span class="hljs-params"><span class="hljs-variable">$password</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">$this</span>-&gt;password = <span class="hljs-variable">$password</span>;<br>    &#125;<br>&#125;<br><br><span class="hljs-variable">$class</span> = <span class="hljs-keyword">new</span> ReflectionClass(<span class="hljs-string">&#x27;Extend\User&#x27;</span>);  <span class="hljs-comment">// 将类名User作为参数，即可建立User类的反射类</span><br><span class="hljs-variable">$properties</span> = <span class="hljs-variable">$class</span>-&gt;getProperties();  <span class="hljs-comment">// 获取User类的所有属性，返回ReflectionProperty的数组</span><br><span class="hljs-variable">$property</span> = <span class="hljs-variable">$class</span>-&gt;getProperty(<span class="hljs-string">&#x27;password&#x27;</span>); <span class="hljs-comment">// 获取User类的password属性ReflectionProperty</span><br><span class="hljs-variable">$methods</span> = <span class="hljs-variable">$class</span>-&gt;getMethods();   <span class="hljs-comment">// 获取User类的所有方法，返回ReflectionMethod数组</span><br><span class="hljs-variable">$method</span> = <span class="hljs-variable">$class</span>-&gt;getMethod(<span class="hljs-string">&#x27;getUsername&#x27;</span>);  <span class="hljs-comment">// 获取User类的getUsername方法的ReflectionMethod</span><br><span class="hljs-variable">$constants</span> = <span class="hljs-variable">$class</span>-&gt;getConstants();   <span class="hljs-comment">// 获取所有常量，返回常量定义数组</span><br><span class="hljs-variable">$constant</span> = <span class="hljs-variable">$class</span>-&gt;getConstant(<span class="hljs-string">&#x27;ROLE&#x27;</span>);   <span class="hljs-comment">// 获取ROLE常量</span><br><span class="hljs-variable">$namespace</span> = <span class="hljs-variable">$class</span>-&gt;getNamespaceName();  <span class="hljs-comment">// 获取类的命名空间</span><br><span class="hljs-variable">$comment_class</span> = <span class="hljs-variable">$class</span>-&gt;getDocComment();  <span class="hljs-comment">// 获取User类的注释文档，即定义在类之前的注释</span><br><span class="hljs-variable">$comment_method</span> = <span class="hljs-variable">$class</span>-&gt;getMethod(<span class="hljs-string">&#x27;getUsername&#x27;</span>)-&gt;getDocComment();  <span class="hljs-comment">// 获取User类中getUsername方法的注释文档</span><br></code></pre></td></tr></table></figure>

<h1 id="交互"><a href="#交互" class="headerlink" title="交互"></a>交互</h1><p>一旦创建了反射类的实例，我们不仅可以通过反射类访问原来类的方法和属性，还能创建原来类的实例或则直接调用类里面的方法。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs php"><span class="hljs-variable">$class</span> = <span class="hljs-keyword">new</span> ReflectionClass(<span class="hljs-string">&#x27;Extend\User&#x27;</span>);  <span class="hljs-comment">// 将类名User作为参数，即可建立User类的反射类</span><br><span class="hljs-variable">$instance</span> = <span class="hljs-variable">$class</span>-&gt;newInstance(<span class="hljs-string">&#x27;youyou&#x27;</span>, <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;***&#x27;</span>);  <span class="hljs-comment">// 创建User类的实例</span><br><br><span class="hljs-variable">$instance</span>-&gt;setUsername(<span class="hljs-string">&#x27;youyou_2&#x27;</span>);  <span class="hljs-comment">// 调用User类的实例调用setUsername方法设置用户名</span><br><span class="hljs-variable">$value</span> = <span class="hljs-variable">$instance</span>-&gt;getUsername();   <span class="hljs-comment">// 用过User类的实例调用getUsername方法获取用户名</span><br><span class="hljs-keyword">echo</span> <span class="hljs-variable">$value</span>;<span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;\n&quot;</span>;   <span class="hljs-comment">// 输出 youyou_2</span><br><br><span class="hljs-variable">$class</span>-&gt;getProperty(<span class="hljs-string">&#x27;username&#x27;</span>)-&gt;setValue(<span class="hljs-variable">$instance</span>, <span class="hljs-string">&#x27;youyou_3&#x27;</span>);  <span class="hljs-comment">// 通过反射类ReflectionProperty设置指定实例的username属性值</span><br><span class="hljs-variable">$value</span> = <span class="hljs-variable">$class</span>-&gt;getProperty(<span class="hljs-string">&#x27;username&#x27;</span>)-&gt;getValue(<span class="hljs-variable">$instance</span>);  <span class="hljs-comment">// 通过反射类ReflectionProperty获取username的属性值</span><br><span class="hljs-keyword">echo</span> <span class="hljs-variable">$value</span>;<span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;\n&quot;</span>;   <span class="hljs-comment">// 输出 youyou_3</span><br><br><span class="hljs-variable">$class</span>-&gt;getMethod(<span class="hljs-string">&#x27;setUsername&#x27;</span>)-&gt;invoke(<span class="hljs-variable">$instance</span>, <span class="hljs-string">&#x27;youyou_4&#x27;</span>); <span class="hljs-comment">// 通过反射类ReflectionMethod调用指定实例的方法，并且传送参数</span><br><span class="hljs-variable">$value</span> = <span class="hljs-variable">$class</span>-&gt;getMethod(<span class="hljs-string">&#x27;getUsername&#x27;</span>)-&gt;invoke(<span class="hljs-variable">$instance</span>);    <span class="hljs-comment">// 通过反射类ReflectionMethod调用指定实例的方法</span><br><span class="hljs-keyword">echo</span> <span class="hljs-variable">$value</span>;<span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;\n&quot;</span>;   <span class="hljs-comment">// 输出 youyou_4</span><br><br><span class="hljs-keyword">try</span> &#123;<br>    <span class="hljs-variable">$property</span> = <span class="hljs-variable">$class</span>-&gt;getProperty(<span class="hljs-string">&#x27;password_1&#x27;</span>);<br>    <span class="hljs-variable">$property</span>-&gt;setAccessible(<span class="hljs-literal">true</span>);   <span class="hljs-comment">// 修改 $property 对象的可访问性</span><br>    <span class="hljs-variable">$property</span>-&gt;setValue(<span class="hljs-variable">$instance</span>, <span class="hljs-string">&#x27;password_2&#x27;</span>);  <span class="hljs-comment">// 可以执行</span><br>    <span class="hljs-variable">$value</span> = <span class="hljs-variable">$property</span>-&gt;getValue(<span class="hljs-variable">$instance</span>);     <span class="hljs-comment">// 可以执行</span><br>    <span class="hljs-keyword">echo</span> <span class="hljs-variable">$value</span>;<span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;\n&quot;</span>;   <span class="hljs-comment">// 输出 password_2</span><br>    <span class="hljs-variable">$class</span>-&gt;getProperty(<span class="hljs-string">&#x27;password&#x27;</span>)-&gt;setAccessible(<span class="hljs-literal">true</span>);    <span class="hljs-comment">// 修改临时ReflectionProperty对象的可访问性</span><br>    <span class="hljs-variable">$class</span>-&gt;getProperty(<span class="hljs-string">&#x27;password&#x27;</span>)-&gt;setValue(<span class="hljs-variable">$instance</span>, <span class="hljs-string">&#x27;password&#x27;</span>);<span class="hljs-comment">// 不能执行，抛出不能访问异常</span><br>    <span class="hljs-variable">$value</span> = <span class="hljs-variable">$class</span>-&gt;getProperty(<span class="hljs-string">&#x27;password&#x27;</span>)-&gt;getValue(<span class="hljs-variable">$instance</span>);   <span class="hljs-comment">// 不能执行，抛出不能访问异常</span><br>    <span class="hljs-variable">$value</span> = <span class="hljs-variable">$instance</span>-&gt;password;   <span class="hljs-comment">// 不能执行，类本身的属性没有被修改，仍然是private</span><br>&#125;<span class="hljs-keyword">catch</span>(<span class="hljs-built_in">Exception</span> <span class="hljs-variable">$e</span>)&#123;<span class="hljs-keyword">echo</span> <span class="hljs-variable">$e</span>;&#125;<br></code></pre></td></tr></table></figure>










            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/PHP/">PHP</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/PHP/">PHP</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/12/31/hello-world/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Hello World</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/05/15/2020%E4%B8%AA%E4%BA%BA%E9%9D%A2%E8%AF%95%E9%A2%98%E6%B1%87%E6%80%BB/">
                        <span class="hidden-mobile">2020个人面试题汇总</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
